from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

from apps.a_common.db import BaseModel
from apps.foundation import db


class User(BaseModel, UserMixin):
    username = db.Column(db.VARCHAR(126))
    password = db.Column(db.VARCHAR(126))

    nickname = db.Column(db.VARCHAR(126), nullable=True)
    first_name = db.Column(db.VARCHAR(126), nullable=True)
    last_name = db.Column(db.VARCHAR(126), nullable=True)
    skills = db.Column(db.ARRAY(db.SmallInteger), nullable=True)
    sex = db.Column(db.SmallInteger, nullable=True)
    work_experience = db.Column(db.Text)
    avatar_store_id = db.Column(db.VARCHAR(126), default='storage/default-avatar.jpg')

    def set_password(self, value):
        self.password = generate_password_hash(value)

    def password_is_true(self, value):
        return check_password_hash(self.password, value)

    @property
    def full_name(self):
        if self.first_name and self.last_name:
            return self.first_name + self.last_name
        elif self.first_name:
            return self.first_name + '先生/女生'
        else:
            return '员工'
